package com.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author Fang Ruichuan
 * @date 2022-09-02 13:14
 */
@Component
public class RedisLockServer {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private RedisLuaConfig redisLuaConfig;

    public boolean setLock(String lockKey, String value, long time, TimeUnit timeUnit) {
        return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lockKey, value, time, timeUnit));
    }

    public void deleteLock(String lockKey, String value) {
        redisLuaConfig.runLuaScript(lockKey, value);
    }

}
